package cn.js.sip.rat.system.controller;

import cn.js.sip.rat.system.dto.AjaxResult;
import cn.js.sip.rat.system.entity.SysMenuEntity;
import cn.js.sip.rat.system.entity.SysRoleEntity;
import cn.js.sip.rat.system.entity.ZtreeEntity;
import cn.js.sip.rat.system.service.SysMenuService;
import cn.js.sip.rat.system.util.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: cn.js.sip.rat.system.controller
 * @Description: TODO
 * @author: xuxiaoli
 * @Date: 2019-12-29   20:24
 */
@Api("菜单管理")
@Controller
@RequestMapping("system/menu")
public class SysMenuController {
    private static Logger logger = LoggerFactory.getLogger(SysMenuController.class);
    private String prefix = "system/menu";
    @Resource(name = "sysMenuService")
    private SysMenuService menuService;

    @ApiOperation("view")
    @RequiresPermissions("system:menu:view")
    @GetMapping
    public String menu() {
        return prefix + "/menu";
    }

    @RequiresPermissions("system:menu:list")
    @PostMapping("/list")
    @ResponseBody
    public List<SysMenuEntity> list(SysMenuEntity menu) {
        List<SysMenuEntity> menuList = menuService.select(menu);
        return menuList;
    }

    @GetMapping("/add/{id}")
    public String toAdd(@PathVariable("id") String id, Map<String, Object> model) {
        SysMenuEntity menu = null;
        if (id.equals("0")) {
            menu = new SysMenuEntity();
            menu.setParentName("0");
            menu.setMenuName("主目录");

        } else {
            menu = menuService.selectMenuById(id);
        }
        model.put("menu", menu);
        return prefix + "/add";
    }


    @GetMapping("/edit/{id}")
    public String toEdit(@PathVariable("id") String id, Map<String, Object> model) {

        SysMenuEntity sysMenuEntity = menuService.selectMenuById(id);

        model.put("menu", sysMenuEntity);
        return prefix + "/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult toEdit(SysMenuEntity sysMenuEntity) {

        menuService.update(sysMenuEntity);

        return AjaxResult.success();
    }

    /**
     * 校验菜单名称
     */
    @PostMapping("/checkMenuNameUnique")
    @ResponseBody
    public String checkMenuNameUnique(SysMenuEntity menu) {
        return menuService.checkMenuNameUnique(menu);
    }

    @RequiresPermissions("system:menu:delete")
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") String id) {
        menuService.delete(id);
        return AjaxResult.success();
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(SysMenuEntity menuEntity) {
        menuService.save(menuEntity);
        return AjaxResult.success();
    }

    /**
     * 加载角色菜单列表树
     */
    @GetMapping("/roleMenuTreeData")
    @ResponseBody
    public List<ZtreeEntity> roleMenuTreeData(SysRoleEntity role) {
        String userId = ShiroUtils.getSysUser().getId();
        List<ZtreeEntity> ztrees = menuService.roleMenuTreeData(role, userId);
        return ztrees;
    }

}
